package com.kuang.common.anno.checklogin.aop;

import com.kuang.common.exception.ValidationException;
import com.kuang.config.UserThreadLocal;
import com.kuang.entity.User;
import com.kuang.handler.jwt.JwtService;
import com.kuang.service.product.UserServiceImpl;
import com.kuang.vo.AuthResponse;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @title: CheckLoginAspect
 * @Author xiaozhen
 * @Date: 2021/9/7 20:22
 * @Version 1.0
 */

@Aspect
@Component
public class CheckLoginAspect {
    private static final Logger log= LoggerFactory.getLogger(CheckLoginAspect.class);

    private static final String AUTH = "Authorization";
    private static final String AUTH_USERNAME = "ksdname";
    @Autowired
    private JwtService jwtService;

    @Autowired
    private UserServiceImpl userService;

    @Pointcut("@annotation(com.kuang.common.anno.checklogin.annotation.CheckLogin)")
    public  void checkLogin(){
        log.info("进行检查");
    }
    @Before("checkLogin()")
    public void beforeCheckLogin(){
        log.info("角色检查");
        this.CheckRole();
    }
    @After("checkLogin()")
    public void  afterCheckLogin(){
        UserThreadLocal.remove();
    }
    public static String getParam(HttpServletRequest request, String filedName) {
        //1 :在参数里面区获取对应filedName的值
        String param = request.getParameter(filedName);
        // 2:如果不存在
        if (StringUtils.isEmpty(param)) {
            // 3: 就请求头区获取对应filedName的值
            param = request.getHeader(filedName);
        }
        return param;
    }
    public void CheckRole(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        String token = getParam(request, AUTH);
        String username = getParam(request, AUTH_USERNAME);
        if (StringUtils.isEmpty(token)) {
            throw new ValidationException(300, "Authorization不允许为空，请重新登录!!!");
        }

        if (StringUtils.isEmpty(username)) {
            throw new ValidationException(300, "username不允许为空，请重新登录!!!");
        }

        // 开始对你token和你用户名进行token校验，如果正常直接返回，如果不正常抛出异常
        AuthResponse authResponse = jwtService.verify(token, username);
        // 如果不等于1，说明token和用户名校验失败
        if (authResponse.getCode() != 1L) {
            log.error("invalid error");
            throw new ValidationException(300, "token valid fail!!!");
        }
        User user = userService.getByUserName(username);
        System.out.println("hello");
        UserThreadLocal.put(user);
    }

}
